﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using StackExchange.Redis;
using Test_StackExchange.Redis.Model;

namespace Test_StackExchange.Redis
{
    class Program
    {
        //包：install-package newtonsoft.json        
        // Install-Package StackExchange.Redis -Version 1.2.6
        static void Main(string[] args)
        {
            //string
            //StringTest test = new StringTest();
            //test.RunRedis();

            //list
            //ListTest test1 = new ListTest();
            //test1.RunRedis();

            //hash
            //HashTest test2 =new HashTest();
            //test2.RunRedis();

            //subscribe
            //Subscribe test3=new Subscribe();
            //test3.RunRedis();
        }

        /// <summary>
        /// 字符串操作
        /// </summary>
        public class StringTest
        {
            public void RunRedis()
            {
                string key = "TestStackExchangeRedisString";
                var currentGuid = Guid.NewGuid().ToString("N");
                RedisHelper.RedisHelper rs = new RedisHelper.RedisHelper();

                //普通字符串存取
                rs.StringSet(key, currentGuid); //存
                //rs.StringSet(key, currentGuid, TimeSpan.FromSeconds(10)); //指定过期时间
                Console.WriteLine(rs.StringGet(key));//取
                Console.WriteLine(rs.StringLenth(key));//长度
                Console.ReadLine();

                //多个元素同时存入
                List<KeyValuePair<RedisKey, RedisValue>> list = new List<KeyValuePair<RedisKey, RedisValue>>();
                list.Add(new KeyValuePair<RedisKey, RedisValue>("TestStackExchangeRedisStringM1","M1"));
                list.Add(new KeyValuePair<RedisKey, RedisValue>("TestStackExchangeRedisStringM2", "M2"));
                rs.StringSet(list); //多个存
                //多取
                var currentMvalue= rs.StringGet(new List<string>() { "TestStackExchangeRedisStringM1" , "TestStackExchangeRedisStringM2" });

                //对象的存取
                Product product = new Product() { Id = 1, Name = "phone" };
                rs.StringSet("objectsave", product);
                Console.WriteLine(rs.StringLenth("objectsave"));
                Console.WriteLine(rs.StringGet<Product>("objectsave").ToString());//也可以直接  rs.StringGet("objectsave")取出来字符串
                Console.ReadLine();
            }
        }

        /// <summary>
        /// 队列（list）操作
        /// </summary>
        public class ListTest
        {
            public void RunRedis()
            {
                string key = "TestStackExchangeRedisList";
                var currentGuid = Guid.NewGuid().ToString("N");
                RedisHelper.RedisHelper rs = new RedisHelper.RedisHelper();

                //单个存取
                rs.ListLeftPush(key, currentGuid);//头存
                rs.ListRightPush(key, Guid.NewGuid().ToString("N"));//尾存
                Console.WriteLine(rs.ListLength(key));//list的count
                var top = rs.ListLeftPop(key);
                var end = rs.ListRightPop(key);
                Console.WriteLine(top);//头取
                Console.WriteLine(end);//尾取
                Console.ReadLine();

                //存取对象
                Product product = new Product() { Id = 2, Name = "car" };
                rs.ListLeftPush(key, product);//存对象
                var currentObj= rs.ListLeftPop<Product>(key).ToString();//取对象
                Console.WriteLine(currentObj);
                Console.ReadLine();

                //多存多取
                rs.EnQueue(key,new List<string>() {"gc","lz","ll"});
                var currentList = rs.DeQueue(key,5);
                currentList.ForEach(x=>Console.WriteLine(x));
                Console.ReadLine();

                //取出所有该key的队列
                //rs.EnQueue(key, new List<string>() { "aa", "vv", "ss" });
                var currentAll = rs.ListRange (key);
                currentAll.ForEach(x => Console.WriteLine(x));
                Console.ReadLine();
            }
        }

        /// <summary>
        /// 哈希（hash）操作
        /// </summary>
        public class HashTest
        {
            public void RunRedis()
            {
                string HashKey = "TestStackExchangeRedisHash";
                var currentGuid = Guid.NewGuid().ToString("N");
                RedisHelper.RedisHelper rs = new RedisHelper.RedisHelper();

                //存取
                string childKey = "child";
                string childKey2 = "brother";
                rs.HashSet(HashKey, childKey, currentGuid);//存
                rs.HashSet(HashKey, childKey2, Guid.NewGuid().ToString("N"));

                var currentValue= rs.HashGet(HashKey, childKey);//取
                Console.WriteLine(currentValue);
                var count = rs.HashLenth(HashKey);//count
                Console.WriteLine(count);
                Console.ReadLine();

                //存取对象
                Product product = new Product() { Id = 3, Name = "food" };
                rs.HashSet<Product>(HashKey, "person", product);
                var currentObj = rs.HashGet<Product>(HashKey, "person");
                Console.WriteLine(currentObj.ToString());
                Console.ReadLine();
            }
        }

        /// <summary>
        /// 订阅subscribe
        /// </summary>
        public class Subscribe
        {
            public void RunRedis()
            {
                string Key = "TestStackExchangeRedisSubscribe";
                var currentGuid = Guid.NewGuid().ToString("N");
                RedisHelper.RedisHelper rs = new RedisHelper.RedisHelper();
                rs.Subscribe(Key);
                rs.Publish(Key, "TestMsg");
                Console.ReadLine();
            }
        }
    }
}
